﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hartley.Entities;
using Hartley.DTOs;

namespace Hartley.BusinessLogic
{

    public class AdminManager
    {
        HCPPADataContext hcDc;
        public AdminManager(HCPPADataContext hcDc)
        {
            this.hcDc = hcDc;
        }

        public void SaveNews(NewsItemDTO newsItemDTO)
        {
           var news =hcDc.hcNews.Where(n => n.NewsId == newsItemDTO.NewsId).SingleOrDefault();
           if (news == null)
           {
               hcNew item = new hcNew { NewsId = newsItemDTO.NewsId, Title = newsItemDTO.Title, Description = newsItemDTO.Description, Type = newsItemDTO.Type, Visible = newsItemDTO.Visible, CreatedBy = newsItemDTO.CreatedBy, CreatedTime = newsItemDTO.CreatedTime, ModifiedBy = newsItemDTO.ModifiedBy,ModifiedTime=newsItemDTO.ModifiedTime };
               hcDc.hcNews.InsertOnSubmit(item);

           }
           
           
           hcDc.SubmitChanges();
        }

                

        public string SaveStaff(StaffDTO staffDTO)
        {
            string result = "";
            bool insertPrincpl = false;
            var staf = hcDc.hcStaffs.Where(n => n.StaffId == staffDTO.StaffId).SingleOrDefault();
            try
            {

                if (staf == null)
                {
                    List<hcStaff> principals = hcDc.hcStaffs.Where(a => a.Role == "Principal").ToList();
                    if (staffDTO.Role == "Principal")
                    {
                        //Check whther there is already a principal with the conflicting teach frm with teach to with the data that is going to be inserted
                        // eg prin1 1980-1985 & prin2 1982 - 1987 cannot happen

                        int newprincipalStartYr = staffDTO.TeachFrom.Value.Year ;
                        int newprincipalEndYr = staffDTO.TeachTo.Value.Year ;
                        List<bool> insertPrincipal = new List<bool>();
                        foreach (hcStaff princpl in principals)
                        {
                            int princplStartYr = princpl.TeachFrom.Value.Year;
                            int princplEndYr = princpl.TeachTo.Value.Year;



                            //create an yr array with the range for the existing princi
                            //List<int> periodOfPrincipal = new List<int>();
                            //for (int i = princplStartYr; i < princplEndYr; i++)
                            //{
                            //    periodOfPrincipal.Add(i);
                            //}
                            if (((newprincipalStartYr >= princplEndYr) && (newprincipalEndYr >= princplEndYr) && princplEndYr != 9999) || ((newprincipalStartYr <= princplStartYr) && (newprincipalEndYr <= princplStartYr) && princplEndYr != 9999))
                            {
                                insertPrincipal.Add(true); 
                                //insertPrincpl = true;
                            }
                           else
                            {
                                //result = "ConflictingPrincipalPeriod";
                                //insertPrincpl = false;
                                insertPrincipal.Add(false); 
                            }


                        }

                        // there canno be more than one principal with 'IsCurrentStaff'
                        //get the count of principal who are having IsCurrentStaff, if it's 0 only thn add new principal as iscurrentstaff

                        if (staffDTO.IsCurrentStaff == true)
                        {
                            if (principals.Where(a => a.IsCurrentStaff == true).ToList().Count == 0)
                            {
                                //insertPrincpl = true;

                                if (insertPrincipal.Contains(false))
                                {
                                    result = "ConflictingPrincipalPeriod";
                                    insertPrincpl = false;
                                }
                                else
                                {
                                    insertPrincpl = true;
                                }
                            }
                            else
                            {
                                result = "currentPrincipalsExceeded";
                                insertPrincpl = false;
                            }
                        }
                        else
                        {
                            if (insertPrincipal.Contains(true))
                            {
                                result = "ConflictingPrincipalPeriod";
                                insertPrincpl = false;
                            }
                            else
                            {
                                insertPrincpl = true;
                            }
                        }

                       

                        if (insertPrincpl)
                        {
                            //principal insertion
                            hcStaff staff = new hcStaff
                            {
                                StaffId = staffDTO.StaffId,
                                FirstName = staffDTO.FirstName,
                                LastName = staffDTO.LastName,
                                DateOfBirth = staffDTO.DateOfBirth,
                                Role = staffDTO.Role,
                                Email = staffDTO.Email,
                                Address = staffDTO.Address,
                                PhoneNumber = staffDTO.PhoneNumber,
                                Subject = staffDTO.Subject,
                                DegreesDetails = staffDTO.DegreesDetails,
                                TeachFrom = staffDTO.TeachFrom,
                                TeachTo = staffDTO.TeachTo,
                                IsCurrentStaff = staffDTO.IsCurrentStaff,
                                Description = staffDTO.Description,
                                PhotoPath = staffDTO.PhotoPath,
                                ModifiedBy = staffDTO.ModifiedBy,
                                ModifiedTime = Convert.ToDateTime(staffDTO.ModifiedTime)
                            };
                            hcDc.hcStaffs.InsertOnSubmit(staff);
                            result = "Saved";
                        }

                    }
                    else
                    {
                        //Normal staff
                        hcStaff staff = new hcStaff
                        {
                            StaffId = staffDTO.StaffId,
                            FirstName = staffDTO.FirstName,
                            LastName = staffDTO.LastName,
                            DateOfBirth = staffDTO.DateOfBirth,
                            Role = staffDTO.Role,
                            Email = staffDTO.Email,
                            Address = staffDTO.Address,
                            PhoneNumber = staffDTO.PhoneNumber,
                            Subject = staffDTO.Subject,
                            DegreesDetails = staffDTO.DegreesDetails,
                            TeachFrom = staffDTO.TeachFrom,
                            TeachTo = staffDTO.TeachTo,
                            IsCurrentStaff = staffDTO.IsCurrentStaff,
                            Description = staffDTO.Description,
                            PhotoPath = staffDTO.PhotoPath,
                            ModifiedBy = staffDTO.ModifiedBy,
                            ModifiedTime = Convert.ToDateTime(staffDTO.ModifiedTime)
                        };
                        hcDc.hcStaffs.InsertOnSubmit(staff);
                        result = "Saved";
                    }

                   

                }

                else
                {
                    //edit
                    result = "Updated";
                }

                hcDc.SubmitChanges();
            }
            catch
            {
                throw;
            }
            return result;
        }








        public List<NewsItemDTO> GetNewsById(Guid NewsId)
        {
            return hcDc.hcNews.Where(a => a.NewsId == NewsId).Select(s => new NewsItemDTO
            {
                NewsId = s.NewsId,
                Title = s.Title,
                Description = s.Description,
                Type = s.Type,
                Visible = s.Visible
            }).ToList();

        }


       
        public List<NewsItemDTO> GetAllNews()
        {

            
            return hcDc.hcNews.Where(b => b.Visible == true).OrderByDescending( c => c.CreatedTime ).Select(s => new NewsItemDTO
            {
                NewsId=s.NewsId,
                Title = s.Title,
                Description = s.Description,
                Type = s.Type,
                Visible = s.Visible,
                NavigationUrl = "viewNews.aspx?" + s.NewsId
            }).ToList();

        }

       

        public List<StaffDTO> GetAllStaff()
        {
            
           /* return hcDc.hcStaffs.Select(s => new StaffDTO
            {
                StaffId = s.StaffId,
                FullName = s.LastName + " " + s.FirstName,
                Address =s.Address,
                DateOfBirth = s.DateOfBirth,
                DegreesDetails = s.DegreesDetails,
                Description = s.Description,
                FirstName = s.FirstName,
                LastName = s.LastName,
                IsCurrentStaff = s.IsCurrentStaff == Convert.ToBoolean(1) ? true : false,
                ModifiedBy =s.ModifiedBy,
                ModifiedTime = s.ModifiedTime,
                PhoneNumber = s.PhoneNumber,
                PhotoPath  =s.PhotoPath,
                Role = s.Role,
                Subject = s.Subject,
                TeachFrom = s.TeachFrom,
                TeachTo = s.TeachTo
                

            }).ToList();*/

            List<hcStaff> allStaff = hcDc.hcStaffs.ToList();
           
            bool isCurrStaff = false;
            List<StaffDTO> staffDTOLst = new List<StaffDTO>();
            foreach(hcStaff staff in allStaff)
            {
                StaffDTO staffDTO = new StaffDTO();
                if (staff.IsCurrentStaff == Convert.ToBoolean(1))
                {
                    isCurrStaff = true;

                    staffDTO.StaffId = staff.StaffId;
                    staffDTO.FullName = staff.LastName + " " + staff.FirstName;
                    staffDTO.Address =staff.Address;
                    staffDTO.DateOfBirth = staff.DateOfBirth;
                    staffDTO.Email = staff.Email;
                    staffDTO.DegreesDetails = staff.DegreesDetails;
                    staffDTO.Description = staff.Description;
                    staffDTO.FirstName = staff.FirstName;
                    staffDTO.LastName = staff.LastName;
                    staffDTO.IsCurrentStaff = isCurrStaff;
                    staffDTO.ModifiedBy =staff.ModifiedBy;
                    staffDTO.ModifiedTime = staff.ModifiedTime;
                    staffDTO.PhoneNumber = staff.PhoneNumber;
                    staffDTO.PhotoPath  =staff.PhotoPath;
                    staffDTO.Role = staff.Role;
                    staffDTO.Subject = staff.Subject;
                    staffDTO.TeachFrom = staff.TeachFrom;
                staffDTO.TeachTo = staff.TeachTo;
                }
                else
                {
                    isCurrStaff = false;

                    staffDTO.StaffId = staff.StaffId;
                    staffDTO.FullName = staff.LastName + " " + staff.FirstName;
                    staffDTO.Address = staff.Address;
                    staffDTO.Email = staff.Email;
                    staffDTO.DateOfBirth = staff.DateOfBirth;
                    staffDTO.DegreesDetails = staff.DegreesDetails;
                    staffDTO.Description = staff.Description;
                    staffDTO.FirstName = staff.FirstName;
                    staffDTO.LastName = staff.LastName;
                    staffDTO.IsCurrentStaff = isCurrStaff;
                    staffDTO.ModifiedBy = staff.ModifiedBy;
                    staffDTO.ModifiedTime = staff.ModifiedTime;
                    staffDTO.PhoneNumber = staff.PhoneNumber;
                    staffDTO.PhotoPath = staff.PhotoPath;
                    staffDTO.Role = staff.Role;
                    staffDTO.Subject = staff.Subject;
                    staffDTO.TeachFrom = staff.TeachFrom;
                    staffDTO.TeachTo = staff.TeachTo;
                }
                staffDTOLst.Add(staffDTO);
            }
            return staffDTOLst;

        }


        public List<StaffDTO> GetStaffById(Guid staffId)
        {

            List<hcStaff> oneStaff = hcDc.hcStaffs.Where(a=>a.StaffId == staffId).ToList();

            bool isCurrStaff = false;
            List<StaffDTO> staffDTOLst = new List<StaffDTO>();
            foreach (hcStaff staff in oneStaff)
            {
                StaffDTO staffDTO = new StaffDTO();
                if (staff.IsCurrentStaff == Convert.ToBoolean(1))
                {
                    isCurrStaff = true;

                    staffDTO.StaffId = staff.StaffId;
                    staffDTO.FullName = staff.LastName + " " + staff.FirstName;
                    staffDTO.Address = staff.Address;
                    staffDTO.Email = staff.Email;
                    staffDTO.DateOfBirth = staff.DateOfBirth;
                    staffDTO.DegreesDetails = staff.DegreesDetails;
                    staffDTO.Description = staff.Description;
                    staffDTO.FirstName = staff.FirstName;
                    staffDTO.LastName = staff.LastName;
                    staffDTO.IsCurrentStaff = isCurrStaff;
                    staffDTO.ModifiedBy = staff.ModifiedBy;
                    staffDTO.ModifiedTime = staff.ModifiedTime;
                    staffDTO.PhoneNumber = staff.PhoneNumber;
                    staffDTO.PhotoPath = staff.PhotoPath;
                    staffDTO.Role = staff.Role;
                    staffDTO.Subject = staff.Subject;
                    staffDTO.TeachFrom = staff.TeachFrom;
                    staffDTO.TeachTo = staff.TeachTo;
                }
                else
                {
                    isCurrStaff = false;

                    staffDTO.StaffId = staff.StaffId;
                    staffDTO.FullName = staff.LastName + " " + staff.FirstName;
                    staffDTO.Address = staff.Address;
                    staffDTO.DateOfBirth = staff.DateOfBirth;
                    staffDTO.Email = staff.Email;
                    staffDTO.DegreesDetails = staff.DegreesDetails;
                    staffDTO.Description = staff.Description;
                    staffDTO.FirstName = staff.FirstName;
                    staffDTO.LastName = staff.LastName;
                    staffDTO.IsCurrentStaff = isCurrStaff;
                    staffDTO.ModifiedBy = staff.ModifiedBy;
                    staffDTO.ModifiedTime = staff.ModifiedTime;
                    staffDTO.PhoneNumber = staff.PhoneNumber;
                    staffDTO.PhotoPath = staff.PhotoPath;
                    staffDTO.Role = staff.Role;
                    staffDTO.Subject = staff.Subject;
                    staffDTO.TeachFrom = staff.TeachFrom;
                    staffDTO.TeachTo = staff.TeachTo;
                }
                staffDTOLst.Add(staffDTO);
            }
            return staffDTOLst;

        }


    

        
    }
}
